******本文研究结果的计算基于python3.87、stata/mp17.0软件*********

***********************基准回归**************************

cd "D:\Statalearning\do\2025\did3\Data"

use data.dta,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(elasticcv)
ddml E[D|X]: pystacked $D $X, type(reg) method(elasticcv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(svm)
ddml E[D|X]: pystacked $D $X, type(reg) method(svm)
ddml crossfit
ddml estimate,rep(md) nocons robust 


use data.dta,clear
global Y gti1
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data.dta,clear
global Y gti2
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 


***********************稳健性检验*********************
*************内生性问题**********
***********工具变量法*********
use data.dta,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
global Z IV
set seed 42 
ddml init iv, kfolds(5)  reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml E[Z|X]: pystacked $Z $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

****根据上面计算结果可知采纳的是训练样本89，然后运用两阶段最小二乘法********
ivreg2 Y1_pystacked_89 (D1_pystacked_89 = Z1_pystacked_89),nocons robust first

***********双重差分法**********
///不考虑控制变量
use data.dta,clear
xtset id year 
global X pgdp uis fin inet hca urban ino enr upm gov ure ums
xtreg gti cbecpolicy i.year, fe r
///考虑控制变量
use data.dta,clear
xtset id year 
global X pgdp uis fin inet hca urban ino enr upm gov ure ums
xtreg gti cbecpolicy $X i.year, fe r
//平行趋势检验
gen aaa=year if cbecpolicy == 1
bys id:egen action1=min(aaa)
gen never=(action1==.)
gen pd=year-action1
replace pd = -6 if pd <= -6
replace pd = 5 if pd >= 5
forvalues i = 6(-1)1{
	gen pre_`i'=(pd==-`i')
}
gen current =(pd==0)
forvalues j = 1(1)5{
	gen las_`j'=(pd==`j')
}
replace pre_6 = 0
eventstudyinteract gti pre_6 pre_5 pre_4 pre_3 pre_2 pre_1 current las_1 las_2 las_3 las_4 las_5,cohort(action1) control_cohort(never) covariates($X) absorb(i.id i.year) vce(robust)

matrix b = e(b_iw)
matrix V = e(V_iw)
ereturn post b V
coefplot m1, baselevels keep (pre* current las_1 las_2 las_3 las_4) vertical yline(0) ytitle("政策效应") xtitle("政策前后") addplot(line @b @at, color(black)) ciopts(recast(rcap) lpattern(dash) msize(medium)) scheme(s1mono) levels(99) coeflabels(pre_6 = "-6" pre_5 = "-5" pre_4 = "-4" pre_3 = "-3" pre_2 = "-2" pre_1 = "-1" current = "0" las_1 = "1" las_2 = "2" las_3 = "3" las_4 = "4" ) xline(7, lp(shortdash)) ylabel(-2 "-2" 0 "0" 2 "2" 4 "4" 6 "6" )  
   
//个体安慰剂检验
clear
mat b=J(500,1,0)
mat se=J(500,1,0)
mat p=J(500,1,0)
forvalues i=1/500{
	use data.dta,clear
	xtset id year
	keep if year == 2014
	sample 98,count
	keep id 
	save matchid.dta,replace
	
	merge 1:m id using data.dta
	gen treat=(_merge==3)
	save matchid`i'.dta,replace
	
	use data.dta,clear
	bsample 1,strata(id)
	keep year
	save matchyear.dta,replace
	mkmat year,matrix(sampleyear)
	
	us matchid`i'.dta,replace
	xtset id year
	gen time=0
	foreach j of numlist 1/277{
	replace time = 1 if(id==`j'&year>=sampleyear[`j',1])
	}
	gen cbecpolicy1=time*treat
	qui xtreg gti cbecpolicy1 pgdp uis fin inet hca urban ino enr upm gov ure ums i.year, fe r
	mat b[`i',1]=_b[cbecpolicy1]
	mat se[`i',1]=_se[cbecpolicy1]
	scalar df_r=e(N)-e(df_m)-1
	mat p[`i',1]=2*ttail(df_r,abs(_b[cbecpolicy1]/_se[cbecpolicy1]))
}
svmat b, names(coef)
svmat se, names(se)
svmat p, names(pvalue)
drop if pvalue1==.
label var pvalue1 p值
label var coef1 估计系数
twoway(scatter pvalue1 coef1, xlabel(-2(0.5)2,grid) yline(0.1,lp(shortdash) lcolor(black)) xline(1.335,lp(shortdash) lcolor(black)) xtitle(估计系数) msymbol(smcircle_hollow) mcolor(black)legend(off)) (kdensity coef1, yaxis(2) legend(off) lcolor(black)), ytitle("p值",axis(1)) ytitle("核密度", axis(2))

*********替换被解释变量衡量指标与改变样本*******
use data,clear 
global Y wy1
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y wy2
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate, rep(md) nocons robust 

use data,clear
drop if id == 2 | id == 176| id == 1| id == 269| id == 39| id == 161| id == 147| id == 171| id == 181
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate, rep(md) nocons robust 

use data,clear
winsor2 gti $X, replace cuts(5 95)
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 


********排除其他试点政策的影响*****
use data,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums lcpolicy i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums gfpolicy i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums ftpolicy i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

**********改变模型参数********
clear
us 数据.dta
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(10) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(201)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

*********************************异质性分析*************************
use data,clear
///Y分别替换为gen\escr\ego\cut\gif
global Y gen
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id 
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 


use data,clear
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums cbecpolicy geol i.year i.id 
global D cbecpolicygeol
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums cbecpolicy gfin i.year i.id 
global D cbecpolicygfin
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gti
global X pgdp uis fin inet hca urban ino enr upm gov ure ums cbecpolicy ugea i.year i.id 
global D cbecpolicyugea
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

************************************中介效应分析********************************************
clear
us 数据.dta
global Y gmc1
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gmc2
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gea1
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gea2
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y ipr
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate, rep(md) nocons robust 

****************************************拓展性分析*********************************
clear
us 数据.dta
global Y tras
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gtcc
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y gtce
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gtft
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y rgtm
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

clear
us 数据.dta
global Y igtm
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gtse
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate,rep(md) nocons robust 

use data,clear
global Y gtsp
global X pgdp uis fin inet hca urban ino enr upm gov ure ums i.year i.id   
global D cbecpolicy
set seed 42 
ddml init partial, kfolds(5) reps(101)
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv)
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv)
ddml crossfit
ddml estimate, rep(md) nocons robust 
